package cn.ysnk.clz.entity.dataUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;

import com.jfinal.plugin.activerecord.Model;

import cn.ysnk.clz.config.annotation.EntityAnnotation;
@EntityAnnotation(tableName="clz_user")
public class DataUser extends Model<DataUser>{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3494625891596235193L;
	public static final DataUser userDao = new DataUser();
	
	public List<DataUser> queryAllUser(){
		return userDao.find("select * from clz_user");
	}
	
	public DataUser findUserByUserName(String username){
		return userDao.findFirst("select * from clz_user where username=?",username);
	}
	
	public SecurityUser switch2User(){
		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
		String[] roleType = getStr("roleType").split("&&&");
		for(String role:roleType){
			GrantedAuthority auth = new GrantedAuthorityImpl(role);
			auths.add(auth);
		}
		SecurityUser u = new SecurityUser(getStr("nickName"),getStr("headIcon"),getStr("username"),getStr("password"), true, true, true,true, auths);
		return u;
	}
}
